using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Lawn.SocketClient.ProtoBase;

namespace Demo
{
    public class Program
    {
        public static async Task Main(string[] args)
        {




            var proto = new ProtobufSerializeProvider();

           //  var msg = new Message()
           //  {
           //      Wt = 100,
           //      CarNo = "test"
           //  };
           // var bs=  proto.Serialize(msg);
           //
           //  Console.WriteLine($"{string.Join(" ",bs)}");
           //
           //  return;;
            
             /*var bs = Encoding.UTF8.GetBytes("topic"); 
            var mm = bs.Select((x, i) => Convert.ToString(x, 16).PadLeft(2, '0'));
            Console.WriteLine(string.Join(" ",mm));
            
            return; */
            
            
            try
            {

              
                
                var  client = new Lawn.SocketClient.EasyClient();

                client.DataReceived += (o, e) =>
                {



                    var data = new byte[e.Length];
                    Buffer.BlockCopy(e.Data, e.Offset, data, 0, e.Length);
                    Console.WriteLine($"datareceive:{Encoding.UTF8.GetString(data)}");
                    
                    
                };
                    
                client.Closed += (o, e) =>
                {
                    
                    Console.WriteLine($"connect close");
                };
                
                client.Connected += (o, e) =>
                {
                    
                    Console.WriteLine("connect");
                    // byte[] b = new byte[]
                    // {
                    //     0x02, 0x00, 0x02, 0x03, 0x01, 0x00
                    // };
                    var msg = new Message()
                     {
                         Wt = 100,
                         CarNo = "test"
                     };
                    
                    //2504000a0006746f70696331000026
                  //  byte[] topic = Encoding.UTF8.GetBytes("topic"+$"{Process.GetCurrentProcess().Id}");
                    //
                    byte[] topic = Encoding.UTF8.GetBytes("topic"+$"{01}");
                    var content = new ProtobufSerializeProvider().Serialize(msg);
                   // var content = Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    List<byte> bytes = new List<byte>();
                    bytes.Add(0x25);
                    bytes.Add(0x03);
                    var totalLen = content.Length+topic.Length + 4;
                    bytes.AddRange(BitConverter.GetBytes((short)totalLen).Reverse());
                    bytes.AddRange(BitConverter.GetBytes((short)topic.Length).Reverse());
                    bytes.AddRange(topic);
                    bytes.AddRange(BitConverter.GetBytes((short)content.Length).Reverse());
                    bytes.AddRange(content);
                    bytes.Add(0x26);
                    
                    var mm = bytes.Select((x, i) => Convert.ToString(x, 16).PadLeft(2, '0'));
                    
                    client.Send(bytes.ToArray());
                    
                    

                };

                
                var execute = false;
                
               var connected=await  client.ConnectAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));

                if (connected)
                {
                    Console.WriteLine("success");
                }
                else
                {
                    Console.WriteLine("failed");
                }
              

                System.Threading.Timer timer = new Timer(x =>
                {
                    Console.WriteLine("execute.....");
                    if (!execute)
                    {
                       
                        execute = true;
                        Thread.Sleep(3000);
                        byte[] topic = Encoding.UTF8.GetBytes("topic"+$"{01}");
                        var content = Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        List<byte> bytes = new List<byte>();
                        bytes.Add(0x25);
                        bytes.Add(0x03);
                        var totalLen = content.Length+topic.Length + 4;
                        bytes.AddRange(BitConverter.GetBytes((short)totalLen).Reverse());
                        bytes.AddRange(BitConverter.GetBytes((short)topic.Length).Reverse());
                        bytes.AddRange(topic);
                        bytes.AddRange(BitConverter.GetBytes((short)content.Length).Reverse());
                        bytes.AddRange(content);
                        bytes.Add(0x26); 
                        // var mm = bytes.Select((x, i) => Convert.ToString(x, 16).PadLeft(2, '0'));
                     
                        if(client.IsConnected)
                            client.Send(bytes.ToArray());
                    
                        
                        Console.WriteLine($"send {DateTime.Now:yyyy-MM-dd HH:mm:ss} ...");
                        execute = false;
                    }
                   

                }, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1));
                 
                 
                     
               
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                 
            }

            Console.ReadLine();

        }
    }
}